package com.tx.admin.util.web;

import java.io.IOException;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class JsonUtil {

	private static ObjectMapper mapper;
	
	static {
		mapper=new ObjectMapper();
	}

	public static String toJson(Object obj)  {

		try {
			return mapper.writeValueAsString(obj);
		} catch (JsonProcessingException e) {
			throw new RuntimeException("格式转换异常:"+obj);
		}
	}
	
	public static <T> T toObject(String json,Class<T> cla) {
		try {
			return mapper.readValue(json,cla);
		} catch (IOException e) {
			throw new RuntimeException("格式转换异常:"+json);
		}
	}
}
